package com.bnq.logc.db

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import java.io.File

class LogcDatabase {
    private lateinit var mSQLiteDatabase: SQLiteDatabase

    fun init(context: Context,dbName:String){
        //打开或创建一个数据库
        val dbRoot = File(context.getExternalFilesDir(null)?:context.cacheDir,
                "logc" +
                File.separator +
                "database")
        if (!dbRoot.exists()) {
            dbRoot.mkdirs()
        }
        val dbFile = File(dbRoot,  "$dbName.db")
        mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, null)
    }

    fun createTable(sql:String){
        mSQLiteDatabase.execSQL(sql)
    }

    fun insert(table:String,values: ContentValues) = mSQLiteDatabase.insert(table,null,values)

    fun query(table:String,distinct:Boolean,columns:Array<String>?,selection:String?,selectionArgs:Array<String>?,groupBy:String?,having:String?,orderBy:String?,limit:String?) =
        mSQLiteDatabase.query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)

    fun delete(table:String,whereClause:String?,whereArgs:Array<String>?) =
        mSQLiteDatabase.delete(table, whereClause, whereArgs)

    fun update(table: String,values:ContentValues,whereClause:String?,whereArgs:Array<String>?) =
        mSQLiteDatabase.update(table, values, whereClause, whereArgs)
}
